<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/12/25
 * Time: 8:36
 */

namespace app\common\command;
use app\common\model\CardBirthdayCare;
use app\common\model\CardTimingPutUser;
use app\common\model\CardUserGet;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Queue;

class Shengricare extends Command
{
    protected function configure()
    {
        $this->setName('shengricare')->setDescription('Here is the remark ');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->shengri();

        $output->writeln("生日关怀发送处理成功:");
    }

    public function shengri()
    {
        $CardTimingPutUser = new CardTimingPutUser();
        $start_time = date('H') . ":00:00";
        $end_time = date('H') . ":59:59";
        $map['notice_time'] = array(array('egt', $start_time), array('elt', $end_time));
        $map['statu'] = "N";
        $CardTimingPutUser->toArray();
        $list = $CardTimingPutUser->where($map)->select();
        $list = collection($list)->toArray();
//        $total = count($list);
//        $e = 1;
        foreach ($list as $k => $v) {
            $ress = $this->task_biz_putbirthday($v);
//            $i = $k;
//            $lists[$k] = $v;
//            if ($i / 50 == 0 && $i > 0 ) {
//                $ress = $this->task_biz_putbirthday($v);
//                $e = $i;
//            }
//
//            if ($total - $e < 50) {
//                $ress = $this->task_biz_putbirthday($v);
//            }
        }
    }

    //发券
    private function task_biz_putbirthday($data = ''){

        // 1.当前任务将由哪个类来负责处理。
        //   当轮到该任务时，系统将生成一个该类的实例，并调用其 fire 方法
        $jobHandlerClassName  = 'app\merchant\job\Birthday';
        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
        $jobQueueName  	  = "task_biz_birthdaycare";
        // 3.当前任务所需的业务数据 . 不能为 resource 类型，其他类型最终将转化为json形式的字符串
        //   ( jobData 为对象时，需要在先在此处手动序列化，否则只存储其public属性的键值对)
        $jobData = $data ;
        // 4.将该任务推送到消息队列，等待对应的消费者去执行
        $isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );
        // database 驱动时，返回值为 1|false  ;   redis 驱动时，返回值为 随机字符串|false
        if( $isPushed !== false ){
            return 'success';
        }else{
            return 'error';
        }
    }

}